from tkinter import *
import time
import datetime
import os

# 设置日志 毫秒保留前三位 14:18:23.978
def log_info():
    return str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]).split(' ')[1] + '：[info] '
def log_error():
    return str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]).split(' ')[1] + '：[error] '
def log_warning():
    return str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]).split(' ')[1] + '：[warning] '

class TK(object):

    def __init__(self, root):       #直接使用root传值
        self.root1 = Frame(root)
        self.root1.pack()
        self.buju()

    def buju(self):
        # 添加一个frame，text与滚动条进行关联
        fm = Frame(root)
        scroll = Scrollbar(fm, orient=VERTICAL)
        self.path_text = Text(fm, font=('宋体', 11),
                         bg='#FFFEEE', fg='green',
                         width=95, height=23,
                         spacing1=5)
        path = Label(root, text='nmon路径：', font=('宋体', 14))
        self.path_entry = Entry(root, show=None, font=('Arial', 12), width=35)

        scroll.config(command=self.path_text.yview)
        self.path_text.config(yscrollcommand=scroll.set)
        scroll.pack(side=RIGHT, fill=Y)
        self.path_text.pack(fill=X, padx=1, pady=10, side=TOP)
        fm.pack()

        path.place(x=20, y=510)
        self.path_entry.place(x=150, y=510)
        #path_text.grid(row=1,  columnspan=2, sticky=W+E+N+S, padx=5, pady=5, )   #合并两行，两列，居中，四周外延5个长度


    def ok(self):

        nmonpath = self.path_entry.get()

        if len(nmonpath) == 0:
            nmonpath = 'None 值'
        #判断输入的路径是否合法
        if ('c:\\' not in nmonpath) and ('d:\\' not in nmonpath) and ('e:\\' not in nmonpath) and ('f:\\' not in nmonpath)\
                 and  ('C:\\' not in nmonpath) and ('D:\\' not in nmonpath) and ('E:\\' not in nmonpath) and ('F:\\' not in nmonpath):
            self.path_text.insert(END, log_error() + '您输入的是 ' + nmonpath + ' ，属非法路径。\n')
            self.root1.update()       #更新输入的内容
            self.path_text.insert(END, log_warning() + '请输入正确的windows路径。\n')       #打印日志
        else:
            self.path_text.insert(END, log_info() + '您输入的路径是：' + nmonpath + '\n')
            self.path_text.see(END)
            self.path_text.focus_force()     #获取光标
            for i in range(10):
                self.path_text.insert(END, log_info() + 'range的结果：' + str(i) + '\n')
                self.root1.update()
                self.path_text.see(END)      #在text控件中自动滚动显示log内容
                time.sleep(1)

    #打开结果目录
    def result(self):

        nmonpath = self.path_entry.get()
        if ('c:\\' not in nmonpath) and ('d:\\' not in nmonpath) and ('e:\\' not in nmonpath) and ('f:\\' not in nmonpath)\
                 and  ('C:\\' not in nmonpath) and ('D:\\' not in nmonpath) and ('E:\\' not in nmonpath) and ('F:\\' not in nmonpath):
            self.path_text.insert(END, log_error() + '您输入的是 ' + nmonpath + ' ，属非法路径。\n')
            self.path_text.see(END)
            self.root1.update()
            time.sleep(0.5)
            self.path_text.insert(END, log_warning() + '请输入正确的windows路径。\n')  # 打印日志
            self.path_text.see(END)
        else:
            self.root1.update()
            self.path_text.insert(INSERT, log_info() + '结果路径：' + nmonpath + '\\result' + '\n')
            self.path_text.see(END)
            os.system('start explorer ' + nmonpath + '\\result')        #打开分析结果文件夹
            self.path_text.insert(INSERT, log_info() + '正在nmon分析结果文件夹，请稍等片刻' + '\n')
            self.path_text.see(END)

    #中止nmon分析过程，考虑使用多线程
    def kill1(self):
        #path_text, path_entry = buju()
        self.path_text.insert(END, log_error() + 'nmon分析过程被人为中断，请重新核对分析过程是否正确，谢谢。\n请重新启动\n')
        self.path_text.see(END)

    def main(self):
        #path_text, path_entry = buju()
        self.path_text.insert(END, '您好，欢迎使用，path：f:\Python\workspace\csvfile\n')

        path_entrybt = Button(root, text='nmon分析', command=self.ok, font=('宋体', 11), fg='green')
        resultbt = Button(root, text='打开结果目录', command=self.result, font=('宋体', 11), fg='green')
        killbt = Button(root, text='中止分析', command=self.kill1, font=('宋体', 11), fg='green')

        path_entrybt.place(x=490, y=504)
        resultbt.place(x=580, y=504)
        killbt.place(x=700, y=504)

if __name__ == '__main__':
    root = Tk()
    root.title('apple')
    root.geometry('800x600')
    root.resizable(False, False)  # 窗口不能缩小 放大

    TK(root).main()     #class传值
    root.mainloop()
